﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Drawing;

namespace HYSoft.Web
{
    public partial class monitor_list1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["uid"] == null || Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            if (!IsPostBack)
            {
                data_show();
                data_gprs();
            }
        }
        //绑定该监测点下的数据
        public void data_show()
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            DataSet ds = new BLL.monitor().GetList(" [index]=" + id + "");

            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                ListView2.DataSource = dt;
                ListView2.DataBind();
                //Repeater2.DataSource = dt;
                //Repeater2.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }
        //绑定该监测点下的传感器信息
        public void data_gprs()
        {
            int curpage = Convert.ToInt32(labPage.Text.ToString());
            PagedDataSource pg = new PagedDataSource();
            int id = int.Parse(Request.QueryString["id"].ToString());
            string sql = " areano=" + id + " order by chuanganqi asc";
            DataSet ds = new BLL.gprsNode().GetList(sql);
            DataTable dt = ds.Tables[0];
            pg.DataSource = ds.Tables[0].DefaultView;
            pg.AllowPaging = true;
            //每页显示的记录条数
            pg.PageSize = 5;
            pg.CurrentPageIndex = curpage - 1;
            this.lnkbtnBack.Enabled = true;
            this.lnkbtnNext.Enabled = true;
            this.lnkbtnOne.Enabled = true;
            this.lnkbtnUp.Enabled = true;
            this.lnkbtnOne.ForeColor = Color.Red;
            this.lnkbtnUp.ForeColor = Color.Red;
            this.lnkbtnNext.ForeColor = Color.Red;
            this.lnkbtnBack.ForeColor = Color.Red;
            //为当前页时，上一页和首页不可点击
            if (curpage == 1 && curpage == pg.PageCount)
            {
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnNext.Enabled = false;
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnOne.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Gray;
                this.lnkbtnUp.ForeColor = Color.Gray;
                this.lnkbtnNext.ForeColor = Color.Gray;
                this.lnkbtnBack.ForeColor = Color.Gray;
            }
            else if (curpage == 1)
            {
                this.lnkbtnOne.Enabled = false;
                this.lnkbtnUp.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Gray;
                this.lnkbtnUp.ForeColor = Color.Gray;
                this.lnkbtnNext.ForeColor = Color.Red;
                this.lnkbtnBack.ForeColor = Color.Red;
            }
            //为最后一页时，最后一页和下一页不可点击
            else if (curpage == pg.PageCount)
            {
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnNext.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Red;
                this.lnkbtnUp.ForeColor = Color.Red;
                this.lnkbtnNext.ForeColor = Color.Gray;
                this.lnkbtnBack.ForeColor = Color.Gray;
            }
           
            //最后一页
            this.labBackPage.Text = Convert.ToString(pg.PageCount);
           
            if (dt.Rows.Count > 0)
            {
                ListView1.DataSource = pg;
                ListView1.DataBind();
                //Repeater1.DataSource = pg;
                //Repeater1.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }

        protected string data_type(object type, object id, object mac)
        {
            int areano = int.Parse(Request.QueryString["id"].ToString());
            string Status = "";
            string sql = " adminno =" + int.Parse(type.ToString()) + "";
            DataSet ds = new BLL.sensor_node().GetList(sql);
            string name = ds.Tables[0].Rows[0]["name"].ToString();
            Status = "<a style=\"color:blue;\" href=\"sensor" + type.ToString() + "_list.aspx?type=" + Server.UrlEncode(type.ToString()) + "&id=" + id + "&areano=" + areano + "&mac=" + int.Parse(mac.ToString()) + "\"><strong>" + "" + name + "" + "</strong></a>";
            //if (type.ToString() == "99")
            //{
            //    Status = "<a style=\"color:blue;\" href=\"show_list1.aspx?type=" + Server.UrlEncode(type.ToString()) + "&id=" + id + "&areano=" + areano + "&mac=" + int.Parse(mac.ToString()) + "\"><strong>" + "摄像头" + "</strong></a>";
            //}
            //else if (type.ToString() == "66")
            //{
            //    Status = "<a style=\"color:blue;\" href=\"show_list2.aspx?type=" + Server.UrlEncode(type.ToString()) + "&id=" + id + "&areano=" + areano + "&mac=" + int.Parse(mac.ToString()) + "\"><strong>" + "位移计" + "</strong></a>";
            //}
            //else if (type.ToString() == "88")
            //{
            //    Status = "<a style=\"color:blue;\" href=\"show_list3.aspx?type=" + Server.UrlEncode(type.ToString()) + "&id=" + id + "&areano=" + areano + "&mac=" + int.Parse(mac.ToString()) + "\"><strong>" + "拉线传感器" + "</strong></a>";
            //}
            //else if (type.ToString() == "55")
            //{
            //    Status = "<a style=\"color:blue;\" href=\"show_list4.aspx?mac=" + mac + "\"><strong>" + "基站" + "</strong></a>";
            //}
            return Status;
        }

        protected string data_chuanganqi(object type)
        {
            string Status = "";
            if (type.ToString() == "正常")
            {
                Status = "<a style=\"color:green;\">" + type + "</a>";
            }
            else if (type.ToString() == "预警")
            {
                Status = "<a style=\"color:orange;\">" + type + "</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">" + type + "</a>";
            }
            return Status;
        }

        protected string data_color(object type)
        {
            string Status = "";
            if (type.ToString() == "正常")

                Status = "<a style=\"color:green;\">" + type + "</a>";
            else
            {
                Status = "<a style=\"color:red;\">" + type + "</a>";
            }

            return Status;
        }

        protected string data_photo(object type)
        {
            string Status = "";
            {
                Status = type.ToString();
            }
            return Status;
        }

        protected string data_Statusid(object id)
        {
            string Status = "";
            string sql = "select name,m.shu,m.adminno from (select adminno,COUNT(*) as shu from gprsNode where areano=" + int.Parse(id.ToString()) + " group by gprsNode.adminno )as m,sensor_node where m.adminno=sensor_node.adminno";
            // comm_data da = new comm_data();
            // DataTable dt = da.backdatatalbeselect("select adminno, count(*) as shu from gprsNode where areano = " + int.Parse(id.ToString()) + " group by adminno");
            DataSet ds = new BLL.gprsNode().GetListadmin(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Status += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[i]["name"].ToString() + "" + dt.Rows[i]["shu"].ToString() + "个<br>";
                    //    if (dt.Rows[i]["adminno"].ToString() == "66")
                    //        Status += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;位移计" + dt.Rows[i]["shu"].ToString() + "个<br>";
                    //    else if (dt.Rows[i]["adminno"].ToString() == "88")
                    //        Status += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;拉线传感器" + dt.Rows[i]["shu"].ToString() + "个<br>";
                    //    else if (dt.Rows[i]["adminno"].ToString() == "99")
                    //        Status += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;摄像头" + dt.Rows[i]["shu"].ToString() + "个<br>";
                    //    else if (dt.Rows[i]["adminno"].ToString() == "55")
                    //        Status += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;基站" + dt.Rows[i]["shu"].ToString() + "个<br>";
                }
            }
            return Status;
        }

        protected string data_Status(object id)
        {
            string Status = "";
            int flag_area = 0; //0正常，1预警，2报警
            string sql = " areano=" + int.Parse(id.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(sql);
            DataTable dt = ds.Tables[0];
            //comm_data da = new comm_data();
            //DataTable dt = da.chektable("gprsNode", "where areano = " + int.Parse(id.ToString()) + "");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["chuanganqi"].ToString() == "报警")
                { flag_area = 2; }

                else if (dt.Rows[i]["chuanganqi"].ToString() == "预警")
                {
                    if (flag_area != 2)
                        flag_area = 1;
                }
            }

            switch (flag_area)
            {
                case 0: Status = "<a style=\"color:green;\">正常</a>"; break;
                case 1: Status = "<a style=\"color:orange;\">预警</a>"; break;
                case 2: Status = "<a style=\"color:red;\">报警</a>"; break;
                default: break;
            }

            return Status;
        }
        //点击首页的事件
        protected void lnkbtnOne_Click(object sender, EventArgs e)
        {
            this.labPage.Text = "1";
            this.data_gprs();
        }
        //点击上一页的事件
        protected void lnkbtnUp_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text.ToString()) - 1);
            this.data_gprs();
        }
        //点击下一页的事件
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text.ToString()) + 1);
            this.data_gprs();
        }
        //最后一页的事件
        protected void lnkbtnBack_Click(object sender, EventArgs e)
        {
            this.labPage.Text = labBackPage.Text;
            this.data_gprs();

        }
    }
}